package com.huawei.admins.platform.gateway.core.config;

import com.huawei.admins.platform.gateway.core.component.filter.FilterDefinition;
import com.huawei.admins.platform.gateway.core.component.router.RouteDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.style.ToStringCreator;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
@ConfigurationProperties("spring.cloud.gateway")
public class GatewayProperties {

    private static final Logger logger = LoggerFactory.getLogger(GatewayProperties.class);

    private List<RouteDefinition> routes = new ArrayList<>();

    private List<FilterDefinition> defaultFilters = new ArrayList<>();

    private List<MediaType> streamingMediaTypes = Arrays.asList(MediaType.TEXT_EVENT_STREAM,
            new MediaType("application", "stream+json"), new MediaType("application", "grpc"),
            new MediaType("application", "grpc+protobuf"), new MediaType("application", "grpc+json"));

    private boolean failOnRouteDefinitionError = true;

    public List<RouteDefinition> getRoutes() {
        return routes;
    }

    public void setRoutes(List<RouteDefinition> routes) {
        this.routes = routes;
        if (routes != null && !routes.isEmpty() && logger.isDebugEnabled()) {
            logger.debug("Routes supplied from Gateway Properties: " + routes);
        }
    }

    public List<FilterDefinition> getDefaultFilters() {
        return defaultFilters;
    }

    public void setDefaultFilters(List<FilterDefinition> defaultFilters) {
        this.defaultFilters = defaultFilters;
    }

    public List<MediaType> getStreamingMediaTypes() {
        return streamingMediaTypes;
    }

    public void setStreamingMediaTypes(List<MediaType> streamingMediaTypes) {
        this.streamingMediaTypes = streamingMediaTypes;
    }

    public boolean isFailOnRouteDefinitionError() {
        return failOnRouteDefinitionError;
    }

    public void setFailOnRouteDefinitionError(boolean failOnRouteDefinitionError) {
        this.failOnRouteDefinitionError = failOnRouteDefinitionError;
    }

    @Override
    public String toString() {
        return new ToStringCreator(this).append("routes", routes).append("defaultFilters", defaultFilters)
                .append("streamingMediaTypes", streamingMediaTypes)
                .append("failOnRouteDefinitionError", failOnRouteDefinitionError).toString();

    }

}
